<?php

namespace App\Models;

use Session;
use Cookie;

class Member extends BaseModel
{
    protected $table = "member";

    const PREFIX = 'mid_';



    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 按条件查询单条数据
     */
    public function getOne(array $where, $fields = '*')
    {
        return $this->multiSelect($fields)->multiWhere($where)->first();
    }

    /**
     * 按条件查询全部数据,根据配置显示条数显示
     */
    public function getList(array $where = [], $fields = '*', $order = '', $pageSize = '')
    {
        if ($pageSize) {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->paginate($pageSize);
        } else {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->get();
        }
    }

    /**
     * 根据条件删除数据
     */
    public function del(array $where)
    {
        return $this->multiWhere($where)->delete();
    }

    /**
     *插入数据
     */
    public function add($data)
    {
        return $this->insertGetId($data);
    }

    /**
     *更新数据
     */
    public function up($where, $data)
    {
        return $this->multiWhere($where)->update($data);
    }

    /**
     * 获取和
     */
    public function getSum($where, $field)
    {
        return $this->multiWhere($where)->sum($field);
    }

    /**
     * 字段递加
     */
    public function inc($where, $field, $n)
    {
        return $this->multiWhere($where)->increment($field, $n);
    }

    public function login($request)
    {
        $where['openid'] = $request['openid'];
        $res = $this->getOne($where);
        if (!$res) {
            return false;
        }
        session(['memberInfo' => $res]);
        return true;
    }

    public function logout()
    {
        Session::forget('mid');
    }


    public function addMember($wxInfo)
    {
        $data['openid'] = $wxInfo['openid'];
        $data['wxname'] = $wxInfo['wxname'];
        $data['wxavater'] = $wxInfo['avater'];
        $data['last_login'] = time();
        $res = $this->add($data);
        if (!$res) {
            return false;
        }
        return true;
    }

    public function findMember($openid)
    {
        $where['openid'] = $openid;
        $member = $this->getOne($where);
        if ($member) {
            $member = $member->toArray();
        }
        return $member;
    }

    public function getMember($mid)
    {
        $key = self::PREFIX . $mid;
        $member = $this->memcache->get($key);
        if (!$member) {
            $member = $this->getOne(['id'=>$mid]);
            if ($member) {
                $member = $member->toArray();
            }
            $this->memcache->set($key, $member);
        }
        return $member;
    }



    public function wechatLogin($openid, $wxInfo = '')
    {
        if ($wxInfo) {
            $this->addMember($wxInfo);
        }
        $data['status'] = 1;
        $member = $this->findMember($openid);
        $key = self::PREFIX . $member['id'];
        if (!$member) {
            $data['status'] = 0;
            return $data;
        }
        $res = $this->memcache->get($key);
        if (!$res) {
            $this->memcache->set($key, $member);
        }
        return $data;
//        session(['mid' => $member['id']]);
//        $_SESSION['mid'] = $member['id'];
    }



}